import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {FetchService} from "../../../core/net/fetch.service";
import {NzMessageService} from "ng-zorro-antd";
import {GeneralSettings} from "../GeneralSettings";
import * as _ from "lodash";
import {RegionMngModel, SearchMaintainService} from "../search-maintain.service";

@Component({
  selector: 'app-mt-well',
  templateUrl: './mt-well.component.html',
  styleUrls: ['./mt-well.component.scss', '../search-maintain.component.scss']
})
export class MtWellComponent implements OnInit {
    pageNum = 1;
    pageSize = 10;
    total: number;
    tableData = [];
    loading = true;
    searchForm: FormGroup;

    modalTitle: string = '';
    modalShow: boolean = false;
    detailForm: FormGroup;
    nzBodyStyle: any = {
        padding: '16px 20px 0'
    };

    props: any;
    commonSelection: RegionMngModel;     latestList: Array<any> = [];
    constructor(private fb: FormBuilder,
                private fetchService: FetchService,
                private message: NzMessageService,
                private searchService: SearchMaintainService,
                private params: GeneralSettings) {
        this.props = this.params;
    }

    ngOnInit() {
        this.searchForm = this.fb.group({
            keywords: []
        });
        this.detailForm = this.fb.group({
            // objectId: [''],
            // sectionName: ['', Validators.required],
            // sectionCode: ['', Validators.required],
            // coordinate: [''],
            // zeroElevation: [''],
            // sectionType: [''],
            // maxStage: [''],
            // sectionDepth: [''],
            // sectionWidth: [''],
            // sectionDesignedFlow: [''],
            // sectionActualFlow: [''],
            // controllingIrrigatedArea: [''],
            // flmlType: ['']
        });
        this.latestList = this.searchService.getLatestList('mtWell');
        this.searchService.regionMngDataChange.subscribe(data => {
            this.commonSelection = data;
            this.getGridData();
        });
    }
    getLatestData(data): void {
        console.log(data);
        this.searchForm.patchValue({
            keywords: data.keyword
        });
        this.getGridData(true);
    }
    openEditModal(item?): void {
        this.modalTitle = item ? `编辑${item.sectionName}` : '新增机井';
        item ? this.detailForm.patchValue(item) : this.detailForm.reset();
        this.modalShow = true;
    }

    onDelete(id) {
        // this.fetchService.get(`wrs/wrsBrcrossSection/remove/${id}`).subscribe(resp => {
        //     this.message.info('删除成功');
        //     this.getGridData();
        // });
    }

    onConfirm() {
        if (!this.detailForm.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = this.detailForm.value;
        if (data.objectId) {
            // this.fetchService.post('wrs/wrsBrcrossSection/edit', data).subscribe(res => {
            //     this.message.success('修改成功');
            //     this.modalShow = false;
            //     this.getGridData();
            // });
        } else {
            // this.fetchService.post('sys/sysRole/add', data).subscribe(res => {
            this.modalShow = false;
            //     this.queryInfo();
            // });
        }
    }

    onCancel() {
        this.modalShow = false;
    }

    getGridData(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        if (this.searchForm.value.keywords) {
            this.searchService.setLatestList('mtWell', this.searchForm.value.keywords);
        }
        this.latestList = this.searchService.getLatestList('mtWell');
        const params = _.assign(_.cloneDeep(this.searchForm.value), this.commonSelection, {
            pageNum: this.pageNum,
            pageSize: this.pageSize
        });
        // this.fetchService.post('wrs/wrsBrcrossSection/list', params).subscribe(res => {
        this.loading = false;
        //     if (res) {
        //         this.total = res['total'];
        //         this.tableData = res['list'];
        //     }
        // });
    }
}
